package com.ukao.cashregister.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.listener.ViewConvertListener;
import com.ukao.cashregister.retrofit.ApiClient;
import com.ukao.cashregister.retrofit.ApiStores;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.widget.ZProgressHUD;

import org.greenrobot.eventbus.EventBus;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

import static android.R.attr.tag;
import static android.R.attr.uiOptions;
import static com.ukao.cashregister.eventbus.PayAmountEvent.Message.show;

/**
 * Created by ukaodev on 2017/6/2.
 */

public class BaseDialogFragment extends DialogFragment{
    protected Activity activity;
    protected  Resources resources;
    protected  ViewConvertListener convertListener;
    protected   static final String ARG_PARAM1 = "param1";
    protected  static final String ARG_PARAM2 = "param2";
    protected  static final String  ARG_PARAM3 = "param3";
    protected   static final String ARG_PARAM4 = "param4";
    protected   static final String ARG_PARAM5 = "param5";
    private int width;//宽度
    private int height;

    protected ZProgressHUD progressDialog;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_FullScreen);
        resources = getActivity().getResources();
        activity = getActivity();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }

    }



    public Resources getResource() {
        return resources;
    }


    public BaseDialogFragment setConvertListener(ViewConvertListener convertListener) {
        this.convertListener = convertListener;
        return this;
    }



    protected OnDialogSureClickListener mOnDialogSureClickListener;

    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    public  int  getColors( @ColorRes int id){
        return   ContextCompat.getColor(activity,id);

    }



    private CompositeSubscription mCompositeSubscription;

    public ApiStores apiStores() {
        return ApiClient.retrofit().create(ApiStores.class);
    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    public void onUnsubscribe() {

        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions())
            mCompositeSubscription.unsubscribe();
    }
    public ZProgressHUD showProgressDialog() {
        if (progressDialog==null){
            progressDialog = new ZProgressHUD(activity);
        }
        progressDialog.show();
        return progressDialog;
    }
    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            // progressDialog.hide();会导致android.view.WindowLeaked
            progressDialog.dismiss();
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        onUnsubscribe();
        progressDialog=null;
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    public  void hideKeyboard(View  mView ) {
        InputMethodManager imm = (InputMethodManager)
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
    }
    public  void hideKeyboard( ) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow( getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    protected String getText(TextView txt) {
        String message = txt.getText().toString().trim();

        return message;
    }


    protected void initListener() {

    }
    protected  boolean useEventBus(){
        return false;
    }








}
